/**
 * 会员Service
 */
package com.kerrykidz.membership.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.ORDER_CONSTANTS;
import com.kerrykidz.common.finger.ValidationFingerPrint;
import com.kerrykidz.filemanagement.dao.model.HisCarCareEntity;
import com.kerrykidz.membership.dao.mapper.CusCouponEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusFngEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusGuaEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemBlackEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemHisEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemSuspendEntityMapper;
import com.kerrykidz.membership.dao.mapper.GblCusCReltypeEntityMapper;
import com.kerrykidz.membership.dao.mapper.GblCusCpndefEntityMapper;
import com.kerrykidz.membership.dao.model.CusCouponEntity;
import com.kerrykidz.membership.dao.model.CusFngEntity;
import com.kerrykidz.membership.dao.model.CusGuaEntity;
import com.kerrykidz.membership.dao.model.CusMemBlackEntity;
import com.kerrykidz.membership.dao.model.CusMemEntity;
import com.kerrykidz.membership.dao.model.CusMemHisEntity;
import com.kerrykidz.membership.dao.model.CusMemSuspendEntity;
import com.kerrykidz.membership.dao.model.GblCusCReltypeEntity;
import com.kerrykidz.membership.dao.model.GblCusCpndefEntity;
import com.kerrykidz.membership.service.IMemberService;
import com.kerrykidz.sales.dao.model.PosOrderEntity;
import com.kerrykidz.sales.dao.model.PosOrderPrdEntity;
import com.kerrykidz.sales.service.IOrderService;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingProductInfoService;
import com.kerrykidz.system.dao.mapper.SecUsrEntityMapper;
import com.kerrykidz.system.util.DateUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;
import com.rdp.util.config.PropertiesUtil;

/**
 * @author Tik
 *
 */
@Service
public class MemberServiceImpl implements IMemberService {
	
	@Autowired
	private CusMemEntityMapper memMapper;
	@Autowired
	private CusGuaEntityMapper guaMapper;
	@Autowired
	private CusFngEntityMapper fngMapper;
	@Autowired
	private SecUsrEntityMapper usrMapper;
	@Autowired
	private GblCusCReltypeEntityMapper reltypeMapper;
	@Autowired
	private CusMemHisEntityMapper cusMemHisEntityMapper;
	@Autowired
	private CusMemBlackEntityMapper cusMemBlackEntityMapper;
	@Autowired
	private CusMemSuspendEntityMapper cusMemSuspendEntityMapper;
	@Autowired
	private CusCouponEntityMapper cpnMapper;
	@Autowired
	private GblCusCpndefEntityMapper cpndefMapper;
	@Autowired
	private GblScmPrdEntityMapper prouductMapper;

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IScmInvocingProductInfoService productService;
	@Autowired
	private CusCouponEntityMapper couponEntityMapper;
	
	/**
	 * 根据姓名或号码查询用户信息
	 * @param nameOrTel
	 * 	当为姓名时，查询会员信息;当为号码时，查询号码对应的监护人所属的会员信息;
	 * @return
	 * @throws RDPException
	 */
	public List<CusMemEntity> findMemberByNameOrTel(String nameOrTel) throws RDPException {
		
		// 判断是否为号码
		boolean isNum;
		if (nameOrTel == null) {
			isNum = false;
		}
		isNum = StringUtils.isNumeric(nameOrTel) ? true : false;
		List<CusMemEntity> list = null;
		
		if  (isNum) {
			list = memMapper.findMemberByTel(nameOrTel);
		} else {
			list = memMapper.findMemberByName(nameOrTel);
		}
		
		return list;
	}
	
	/**
	 * 根据姓名或号码查询用户信息（现场）
	 * @param nameOrTel
	 * 	当为姓名时，查询会员信息;当为号码时，查询号码对应的监护人所属的会员信息;
	 * @return
	 * @throws RDPException
	 */
	public List<CusMemEntity> findSiteMemberByNameOrTel(String nameOrTel, String fkBranId) throws RDPException {
		
		// 判断是否为号码
		boolean isNum;
		if (nameOrTel == null) {
			isNum = false;
		}
		isNum = StringUtils.isNumeric(nameOrTel) ? true : false;
		List<CusMemEntity> list = null;
		
		if  (isNum) {
			list = memMapper.findSiteMemberByTel(nameOrTel, fkBranId);
		} else {
			list = memMapper.findSiteMemberByName(nameOrTel, fkBranId);
		}
		
		return list;
	}
	
	/**
	 * 查询指纹对应人员信息
	 * @param nameOrTel
	 * 	指纹数据
	 * @return
	 * 	如果为会员、非会员则返回CusMemEntity
	 * 	如果为监护人则返回CusGuaEntity
	 * 	如果为销售则返回SecUsrEntity
	 * @throws RDPException
	 */
	public Object findFingerprint(String fingerprint) throws RDPException {
		
		List<CusFngEntity> list = fngMapper.findFingerprint();
		if (list == null || list.size() == 0) return null;
		// 为提高比对命中率真，先比对主手指，不匹配再比对副手指
		// 比对主手指
		for (CusFngEntity fng : list) {
			// 指纹比对
			if (ValidationFingerPrint.userMatch(fingerprint, fng.getFing1()) == 0)  {
				if (fng.getFkMemId() != null) {
					return memMapper.findEntityById(fng.getFkMemId());
				} else if (fng.getFkGuaId() != null) {
					return guaMapper.findEntityById(fng.getFkGuaId());
				} else if (fng.getFkUsrId() != null) {
					return usrMapper.findEntityById(fng.getFkUsrId());
				}
			}
		}
		
		// 比对副手指
		for (CusFngEntity fng : list) {
			// 指纹比对
			if (ValidationFingerPrint.userMatch(fingerprint, fng.getFing2()) == 0)  {
				if (fng.getFkMemId() != null) {
					return memMapper.findEntityById(fng.getFkMemId());
				} else if (fng.getFkGuaId() != null) {
					return guaMapper.findEntityById(fng.getFkGuaId());
				} else if (fng.getFkUsrId() != null) {
					return usrMapper.findEntityById(fng.getFkUsrId());
				}
			}
		}
		return null;
	}

	/**
	 * 根据会员ID查询会员
	 */
	@Override
	public CusMemEntity findMemberById(String memberId) throws RDPException {
		CusMemEntity member = memMapper.findEntityById(memberId);
		return member;
	}

	/**
	 * 根据会员ID查询会员及其兄弟姐妹
	 * 
	 * @param memberId 会员编号
	 * 
	 */
	@Override
	public List<CusMemEntity> findMemberAndSiblingsById(String memberId)
					throws RDPException {
		List<CusMemEntity> list = memMapper.findMemberAndSiblingsById(memberId);
		return list;
	};
	
	/**
	 * 根据会员ID查询会员及其兄弟姐妹（现场）
	 * 
	 * @param memberId 会员编号
	 * 
	 */
	@Override
	public List<CusMemEntity> findSiteMemberAndSiblingsById(String memberId)
					throws RDPException {
		List<CusMemEntity> list = memMapper.findSiteMemberAndSiblingsById(memberId);
		return list;
	};

	/**
	 * 根据会员编号查询监护人列表
	 * 
	 * @param memberId 会员编号
	 */
	@Override
	public  List<CusGuaEntity> findGuaByMemberId(String memberId) throws RDPException {
		 List<CusGuaEntity> list = guaMapper.findGuaByMemberId(memberId);
		return list;
	}
	
	/**
	 * 根据会员编号查询监护人列表(现场)
	 * 
	 * @param memberId 会员编号
	 */
	@Override
	public  List<CusGuaEntity> findSiteGuaByMemberId(String memberId) throws RDPException {
		 List<CusGuaEntity> list = guaMapper.findSiteGuaByMemberId(memberId);
		return list;
	}

	/**
	 * 取得会员玩伴券
	 * @param memberId 会员编号
	 * @return
	 * @throws RDPException
	 */
	public List<Map<String, Object>> findVoucherCouponsByMemberId(String memberId) throws RDPException {
		 List<Map<String, Object>> list = memMapper.findVoucherCouponsByMemberId(memberId);
		 return list;
	}
	
	/**
	 * 取得所有关系类型
	 * @return
	 * @throws RDPException
	 * @date: 2014年8月15日
	 */
	public List<GblCusCReltypeEntity> findReltypes() throws RDPException {
		
		 List<GblCusCReltypeEntity> list = reltypeMapper.selectAll();
		 return list;
	}

	/**
	 * 删除会员
	 * @param memberId
	 * @throws RDPException
	 * @date: 2014年8月15日
	 */
	@Override
	public void deleteMember(CusMemEntity member) throws RDPException {
		memMapper.deleteEntity(member);
		CusFngEntity fng = new CusFngEntity();
		fng.setFkMemId(member.getPkMemId());
		fngMapper.deleteEntity(fng);
	}

	
	/**
	 * 验证指纹是否已存在
	 * @param fingerprint
	 * @return int
	 * 	0:不存在 1：会员 2：监护人 3：销售
	 * @throws RDPException
	 * @date: 2014年8月17日
	 */
	@Override
	public int checkFingerprintExist(String fingerprint) throws RDPException {
		
		if (StringUtils.isBlank(fingerprint)) return 0;
		
		List<CusFngEntity> list = fngMapper.findFingerprint();
		if (list.size() == 0) return 0;
		
		// 为提高比对命中率真，先比对主手指，不匹配再比对副手指
		// 比对主手指
		for (CusFngEntity fng : list) {
			// 指纹比对
			if (ValidationFingerPrint.userMatch(fingerprint, fng.getFing1()) == 0)  {
				if (fng.getFkMemId() != null) {
					return 1;
				} else if (fng.getFkGuaId() != null) {
					return 2;
				} else if (fng.getFkUsrId() != null) {
					return 3;
				}
			}
		}
		for (CusFngEntity fng : list) {
			// 指纹比对
			if (ValidationFingerPrint.userMatch(fingerprint, fng.getFing2()) == 0)  {
				if (fng.getFkMemId() != null) {
					return 1;
				} else if (fng.getFkGuaId() != null) {
					return 2;
				} else if (fng.getFkUsrId() != null) {
					return 3;
				}
			}
		}
		return 0;
	}

	@Override
	public List<CusMemEntity> findByGua(String guaId) {
		return this.memMapper.findByGua(guaId);
	}

	public List<HisCarCareEntity> getCarFormHisList(List<String> memberIdList) {
		return memMapper.getCarFormHisList(memberIdList);
	}

	/**
	 * 修改会员会员
	 * @param memberId
	 * @throws RDPException
	 * @date: 2014年8月15日
	 */
	@Override
	public void updateMember(CusMemEntity member) throws RDPException {
		if(Constants.MEMBER_STATUS_STOPCARD.equals(member.getStatus())){
			//停卡时改状态
			//如果要求停卡日期是今天，则直接停//否则后台定时器停
			if(null!=member.getSuspendStart() && (member.getSuspendStart().equals(DateUtil.formatDate("yyyy/MM/dd", new Date())))){
				memMapper.updateEntity(member);
			}else{
				member.setStatus(null);
				memMapper.updateEntity(member);
				member.setStatus(Constants.MEMBER_STATUS_STOPCARD);
			}
		}
		if(null!=member && null!=member.getStatus()){
			if(Constants.MEMBER_STATUS_STOPCARD.equals(member.getStatus())){
				//停卡功能加入停卡记录表(停卡时间段不同，只增不改)
				CusMemSuspendEntity cusMemSuspend = new CusMemSuspendEntity();
				cusMemSuspend.setPkSuspendId(GenerationUUID.getGenerationUUID());
				cusMemSuspend.setFkMemId(member.getPkMemId());
				
				//运算停卡时间段
				String suspendStart=member.getSuspendStart();
				String suspendEnd=member.getSuspendEnd();
				if(!StringUtils.isBlank(suspendStart) && !StringUtils.isBlank(suspendEnd)){
					//都选了
					cusMemSuspend.setSuspendStart(DateUtil.getDate(suspendStart));
					cusMemSuspend.setSuspendEnd(DateUtil.getDate(suspendEnd));
				}else if(!StringUtils.isBlank(suspendStart) && StringUtils.isBlank(suspendEnd)){
					//只选了第一个
					cusMemSuspend.setSuspendStart(DateUtil.getDate(suspendStart));
					cusMemSuspend.setSuspendEnd(DateUtil.addDay2Date(60, DateUtil.getDate(suspendStart)));
				}else if(StringUtils.isBlank(suspendStart) && !StringUtils.isBlank(suspendEnd)){
					//只选了第二个
					cusMemSuspend.setSuspendEnd(DateUtil.getDate(suspendEnd));
					cusMemSuspend.setSuspendStart(DateUtil.addDay2Date(-60, DateUtil.getDate(suspendEnd)));
				}
				
				cusMemSuspend.setSuspendReason(member.getSuspendReason());
				cusMemSuspend.setFkMngFile(member.getFkMngFile());
				cusMemSuspend.setFkBranId(member.getFkBranId());
				cusMemSuspend.setModiBy(member.getModiBy());
				cusMemSuspend.setModiTime(member.getModiTime());
				cusMemSuspendEntityMapper.saveEntity(cusMemSuspend);
				
				//停卡操作修改会员历史记录有效期
				String hisType=null;
				if(null!=member.getMemType() && "1".equals(member.getMemType())){
					hisType=Constants.MEM_HIS_TYPE_MEMBER;
				}else{
					hisType=Constants.MEM_HIS_TYPE_TICKET;
				}
				CusMemHisEntity cusMemHis = cusMemHisEntityMapper.findEntityByMembId(member.getPkMemId(), hisType);//修改历史表
				cusMemHis.setEndDate(member.getEndDate());
				cusMemHisEntityMapper.updateEntityByMembId(cusMemHis);
				
				//修改玩伴券有效期
				couponEntityMapper.updateCouponBymemId(member.getPkMemId(), member.getStartDate(), member.getEndDate());
			}else {
				CusMemBlackEntity cusMemBlack=new CusMemBlackEntity();
				cusMemBlack.setFkMemId(member.getPkMemId());
				cusMemBlack.setFkBranId(member.getFkBranId());
				cusMemBlack.setModiBy(member.getModiBy());
				cusMemBlack.setIsBlack("1");
				cusMemBlack.setBlackReason(member.getBlackReason());
				
				//先查询，没有就新增，有就修改
				CusMemBlackEntity cusMemBlackEntity=cusMemBlackEntityMapper.findEntityByMembId(member.getPkMemId());
				if(null!=cusMemBlackEntity){
					//黑名单功能修改
					cusMemBlackEntityMapper.updateEntityByMembId(cusMemBlack);
				} else {
					//黑名单功能加入
					cusMemBlackEntityMapper.saveEntity(cusMemBlack);
				}
			}
		}
	}

	/**
	 * 取消停卡
	 * @throws RDPException
	 * @YJJ
	 */
	public void cancelStopCard(CusMemEntity member, CusMemSuspendEntity cusMemSuspend) throws RDPException {
		memMapper.updateEntity(member);
		cusMemSuspendEntityMapper.updateEntity(cusMemSuspend);
		
		//停卡操作修改会员历史记录有效期
		if(null!=member){
			String hisType=null;
			if(null!=member.getMemType() && "1".equals(member.getMemType())){
				hisType=Constants.MEM_HIS_TYPE_MEMBER;
			}else{
				hisType=Constants.MEM_HIS_TYPE_TICKET;
			}
			CusMemHisEntity cusMemHis = cusMemHisEntityMapper.findEntityByMembId(member.getPkMemId(), hisType);//修改历史表
			cusMemHis.setEndDate(member.getEndDate());
			cusMemHisEntityMapper.updateEntityByMembId(cusMemHis);
			
			//修改玩伴券有效期
			couponEntityMapper.updateCouponBymemId(member.getPkMemId(), member.getStartDate(), member.getEndDate());
		}
	}
	/**
	 * 创建会员历史记录（收银后调用）
	 * @param orderId
	 * @throws RDPException
	 * @date: 2014年8月27日
	 */
	@Override
	public void createMemHis(String orderId) throws RDPException {

		PosOrderEntity order =  orderService.findOrderByPk(orderId);
		if (!ORDER_CONSTANTS.ORDER_STATUS_HAS_BEEN_PAY.equals(order.getOrderStatus())) {
			return;
		}
		List<PosOrderPrdEntity> prds = orderService.findOrderPrdByOrderId(orderId);
		
		List<CusMemHisEntity> list = new ArrayList<CusMemHisEntity>();
		if (ORDER_CONSTANTS.ORDER_TYPE_TICKET.equals(order.getOrderType())) {
			for (PosOrderPrdEntity prd : prds) {
				
				//充值记录忽略
				if (ORDER_CONSTANTS.ORDER_PRD_TYPE_CHARGE.equals(prd.getType())) continue;
				
				/** 生成购买单次票历史 */
				CusMemHisEntity his = new CusMemHisEntity();
				his.setPkMemhisId(GenerationUUID.getGenerationUUID());
				// 会员或非会员编号，关联CUS_MEM
				his.setFkMemId(prd.getFkMemId());
				// 历史记录的类型
				his.setHisType(Constants.MEM_HIS_TYPE_TICKET);
				// 关联的订单编号，关联POS_ORDER表
				his.setFkOrderId(orderId);
				// 会籍或单次票商品编号，关联GBL_SCM_PRD表
				his.setFkPrdId(prd.getFkPrdId());
				// 使用玩伴券购买单次票 =1 使用，=0 不使用
				his.setCouponFlag(prd.getCouponFlag());
				// 订单使用的玩伴券编号
				his.setFkBdycpnId(prd.getFkBdycpnId());
				// 是否使用会员续会优惠券标记 =1 是
				his.setExtcpnFlag("0");
				// 会籍或单次票使用状态 =1 未使用（有效） =0 已使用或失效 =2 草稿
				his.setUserStatus("1");
				his.setModiBy(order.getModiBy());
				list.add(his);
				
				CusMemEntity cusMem = this.findMemberById(prd.getFkMemId());
				/** 非会员购买单次票1+1时，更新成非会员状态，会员不更新*/
				if (!Constants.MEM_TYPE_MEMBER.equals(cusMem.getMemType())) {
					GblScmPrdEntity single = productService.findSingleTicket(prd.getFkPrdId());
					if (single != null && single.getChdCnt() == 1) {
						// 购买商品为单次票1+1时，更新为非会员
						CusMemEntity member = new CusMemEntity();
						member.setPkMemId(prd.getFkMemId());
						member.setMemType(Constants.MEM_TYPE_NOTMEMBER);
						member.setStatus(Constants.MEMBER_STATUS_VALID);
						member.setModiBy(order.getModiBy());
						memMapper.updateEntity(member);
					}
					 /**YJJ  新建和过期会员购买单次票1+1时，更新成非会员状态，状态为有效*/
				}else if ((Constants.MEM_TYPE_MEMBER.equals(cusMem.getMemType())) && (Constants.MEMBER_STATUS_OVERDUE.equals(cusMem.getStatus()) || Constants.MEMBER_STATUS_CREATE.equals(cusMem.getStatus()))) {
						GblScmPrdEntity single = productService.findSingleTicket(prd.getFkPrdId());
						if (single != null && single.getChdCnt() == 1) {
							// 购买商品为单次票1+1时，更新为非会员
							CusMemEntity member = new CusMemEntity();
							member.setPkMemId(prd.getFkMemId());
							member.setMemType(Constants.MEM_TYPE_NOTMEMBER);
							member.setStatus(Constants.MEMBER_STATUS_VALID);
							member.setModiBy(order.getModiBy());
							memMapper.updateEntity(member);
						}
					}
				/** 玩伴券使用历史*/
				if (StringUtils.isNotBlank(prd.getFkBdycpnId())) {
					CusCouponEntity cpn = new CusCouponEntity();
					cpn.setPkCouponId(prd.getFkBdycpnId());
					cpn.setUsedBy(prd.getFkMemId());
					cpn.setUsedTime(new Date());
					cpn.setStatus("2");
					cpnMapper.updateEntity(cpn);
				}
			}
			cusMemHisEntityMapper.saveBatch(list);
			
		} else if (ORDER_CONSTANTS.ORDER_TYPE_VIP.equals(order.getOrderType())) {
			
			// 退会
			if (ORDER_CONSTANTS.RET_FLAG_YES.equals(order.getRetFlag())) {
				CusMemEntity member = new CusMemEntity();
				for (PosOrderPrdEntity prd : prds) {
					//充值记录忽略
					if (ORDER_CONSTANTS.ORDER_PRD_TYPE_CHARGE.equals(prd.getType())) continue;
					member.setPkMemId(prd.getFkMemId());
					member.setStatus(Constants.MEMBER_STATUS_OVERDUE);
					member.setEndDate(new Date());
					member.setBalance(0d);
					member.setModiBy(order.getModiBy());
					memMapper.updateEntity(member);

					CusMemHisEntity his = cusMemHisEntityMapper.findMemberHisByMembId(prd.getFkMemId());
					//his.setEndDate(new Date());
					his.setUserStatus("0");
					his.setModiBy(order.getModiBy());
					cusMemHisEntityMapper.updateEntityByRetreat(his);
					
					CusMemHisEntity hisAdd = new CusMemHisEntity();
					hisAdd.setPkMemhisId(GenerationUUID.getGenerationUUID());
					hisAdd.setFkMemId(prd.getFkMemId());
					hisAdd.setHisType(Constants.MEM_HIS_TYPE_RET);
					hisAdd.setStartDate(new Date());
					hisAdd.setEndDate(his.getEndDate());
					hisAdd.setFkOrderId(orderId);
					hisAdd.setFkPrdId(his.getFkPrdId());
					hisAdd.setUserStatus("0");
					hisAdd.setModiBy(order.getModiBy());
					cusMemHisEntityMapper.saveEntity(hisAdd);
				}
				return;
			}
			
			// 购买会籍
			for (PosOrderPrdEntity prd : prds) {

				//充值记录忽略
				if (ORDER_CONSTANTS.ORDER_PRD_TYPE_CHARGE.equals(prd.getType())) continue;
				
				/** 生成购买会籍历史 */
				CusMemHisEntity his = new CusMemHisEntity();
				his.setPkMemhisId(GenerationUUID.getGenerationUUID());
				his.setFkMemId(prd.getFkMemId());
				his.setHisType(Constants.MEM_HIS_TYPE_MEMBER);
				// 关联的订单编号，关联POS_ORDER表
				his.setFkOrderId(orderId);
				// 会籍或单次票商品编号，关联GBL_SCM_PRD表
				his.setFkPrdId(prd.getFkPrdId());
				// 使用玩伴券购买单次票 =1 使用，=0 不使用
				his.setCouponFlag(prd.getCouponFlag());
				// 订单使用的玩伴券编号
				his.setFkBdycpnId(prd.getFkBdycpnId());
				// 是否使用会员续会优惠券标记 =1 是
				his.setExtcpnFlag(prd.getExtcpnFlag());
				his.setUserStatus("1");
				his.setModiBy(order.getModiBy());
				list.add(his);
				
				/** 更新会员状态 */
				CusMemEntity member=memMapper.findEntityById(prd.getFkMemId());
				member.setMemType(Constants.MEM_TYPE_MEMBER);
				//判断续会情况，如果已是有效期，则不改状态
				if(null==member.getStatus() || !member.getStatus().equals(Constants.MEMBER_STATUS_VALID)){
					member.setStatus(Constants.MEMBER_STATUS_NOTACTIVE);
				}
				member.setModiBy(order.getModiBy());
				memMapper.updateEntity(member);
				
				/** 发放玩伴券 */
				GblScmPrdEntity product = new GblScmPrdEntity();
				product.setPkPrdId(prd.getFkPrdId());
				GblScmPrdEntity vip = prouductMapper.findMemberProduct(product);
				if (vip == null) continue;
				
				// 取得优惠券张数
				PropertiesUtil prop = new PropertiesUtil();
				String strCnt = prop.readSingleProps("VIP_MONTH_" + vip.getMonth(), "/properties/kerrykidz.properties");
				if (strCnt == null) continue;
				
				Integer cnt = Integer.parseInt(strCnt);
				List<GblCusCpndefEntity> cpndefList = cpndefMapper.findCouponsByResverseType(Constants.PRODUCT_TYPE_SINGLE);
				if (cpndefList.size() > 0) {
					for (int i = 0; i < cnt; i ++) {
						CusCouponEntity cpn = new CusCouponEntity();
						cpn.setPkCouponId(GenerationUUID.getGenerationUUID());
						cpn.setFkMemId(prd.getFkMemId());
						cpn.setFkCpndefId(cpndefList.get(0).getPkCpndefId());
						cpn.setStatus("1");
						cpn.setFkBranId(order.getFkBranId());
						cpnMapper.saveEntity(cpn);
					}
				}
				
			}
			cusMemHisEntityMapper.saveBatch(list);
		}
		
	}
	
	/**
	 * 根据姓名或号码查询用户信息(现场)
	 * @param nameOrTel
	 * 	当为姓名时，查询会员信息;当为号码时，查询号码对应的监护人所属的会员信息;
	 * @return
	 * @throws RDPException
	 */
	public List<CusMemEntity> findMemberByNameOrTelAndSale(String nameOrTel, String userId, String fkBranId) throws RDPException {
		
		// 判断是否为号码
		boolean isNum;
		if (nameOrTel == null) {
			isNum = false;
		}
		isNum = StringUtils.isNumeric(nameOrTel) ? true : false;
		List<CusMemEntity> list = null;
		
		if  (isNum) {
			list = memMapper.findMemberByTelAndSale(nameOrTel, userId, fkBranId);
		} else {
			list = memMapper.findMemberByNameAndSale(nameOrTel, userId, fkBranId);
		}
		
		return list;
	}

	/**
	 * 取得该会员带了几个非会员购单次票
	 * @param memberId
	 * @throws RDPException
	 * @date: 2014年9月11日
	 */
	@Override
	public Integer checkBdyCount(String memberId) throws RDPException {
		return cusMemHisEntityMapper.checkBdyCount(memberId);
	}

	/**
	 * 验证指定会员是否还有未使用的单次票
	 * @param memberId
	 * @throws RDPException
	 * @date: 2014年11月13日
	 */
	@Override
	public boolean checkEnableSingleByMemberId(String memberId) throws RDPException {
		Integer count = cusMemHisEntityMapper.findEnableSingleByMemberId(memberId);
		return count > 0 ?  true : false;
	}
	

	/**
	 * 会员失效、取消停卡，每天执行一次
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@Override
	public void doQuarz() throws RDPException {
		// 昨天或昨天之前有效期到期的会员状态设为“过期”
		memMapper.updateInvalidMemberByQuartz();
		cusMemHisEntityMapper.updateInvalidMemberByQuartz();
		// 新增过期会员历史余额清0记录
		cusMemHisEntityMapper.insertExpireStoredByQuartz();
		// 更新过期会员余额为0记录
		memMapper.updateExpireStoredByQuartz();
		
		// 明天或明天之后停卡到停卡时间的会员状态设为“停卡”
		memMapper.updateMemberToStopcardByQuartz();
		
		// 昨天或昨天之前停卡期到期的停卡会员状态设为“有效”
		memMapper.updateStopcardMemberByQuartz();
		
	}

	public void updateEntity(CusMemSuspendEntity cusMemSuspendEntity) throws RDPException {
		cusMemSuspendEntityMapper.updateEntity(cusMemSuspendEntity);
	}

	@Override
	public CusMemEntity getRetreatMember(String memberId)
					throws RDPException {
		return memMapper.findRetreatMember(memberId);
	}

	/**
	 * 退会
	 * @param memberId
	 * @param memberId
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@Override
	public void retreatMember(CusMemEntity member) throws RDPException {
		memMapper.updateEntity(member);
	}

	@Override
	public CusMemEntity findMemberByMemName(String memName) throws RDPException {
		List<CusMemEntity> member = memMapper.findMemberByMemName(memName);
		return member.get(0);
	}
}
